using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Web.Security;
using StructureMap;

using Impact.Infrastructure.Plugins;
using Impact.Infrastructure.Exceptions;
using Impact.Model.Objects;
using Impact.Model.Service;
using System.Web.UI.HtmlControls;
using System.Web.UI;

namespace Impact.Plugins
{
    /// <summary>
    /// Plugin that randomly selects an article from a category
    /// </summary>
    public class ArticleRandomizerPlugin : IImpactPlugin
    {
        private static Guid id = new Guid( "D7FDEF44-D6D2-40ea-9A20-1F11EB8A29D5" );
        private ArticlePluginAdmin _admin;

        /// <summary>
        /// CategoryId parameter name
        /// </summary>
        public const string CategoryId = "CategoryId";
        /// <summary>
        /// Headline parameter name
        /// </summary>
        public const string Headline = "Headline";


        /// <summary>
        /// default constructor
        /// </summary>
        public ArticleRandomizerPlugin()
        {
            _admin = new ArticlePluginAdmin();
        }


        #region IImpactPlugin Members

        /// <summary>
        /// unique ID of this plugin
        /// </summary>
        public Guid PluginId
        {
            get { return id; }
        }

        /// <summary>
        /// friendly string name of this plugin
        /// </summary>
        public string Name
        {
            get { return "Display Random Article"; }
        }

        /// <summary>
        /// list of parameters this plugin uses 
        /// </summary>
        public Dictionary<string, PluginParameter> Parameters
        {
            get
            {
                Dictionary<string, PluginParameter> output = new Dictionary<string, PluginParameter>();
                output.Add( CategoryId, new PluginParameter( this, CategoryId, typeof( Int32 ), true ) );
                output.Add( Headline, new PluginParameter( this, Headline, typeof( string ), false ) );
                return output;
            }
        }

        /// <summary>
        /// gets administrative interface used for this plugin 
        /// </summary>
        public IPluginAdmin Admin
        {
            get { return _admin; }
        }

        /// <summary>
        /// using the given parameter values, renders the plugin as an Html control
        /// </summary>
        public void Render( Control container, Dictionary<string, PluginParameter> parameterValues )
        {
            PluginParameter categoryIdParameter = parameterValues[CategoryId];
            PluginParameter headlineParameter = parameterValues[Headline];

            if ( categoryIdParameter.IsRequired && string.IsNullOrEmpty( categoryIdParameter.Value ) )
            {
                throw new ArgumentException( "Category ID is required" );
            }

            int categoryId = Int32.Parse( categoryIdParameter.Value );

            IArticleService articleService = ObjectFactory.GetInstance<IArticleService>();
            ArticleCollection articles = articleService.FetchByCategory( categoryId, true );
            if ( articles.Count > 0 )
            {

                HtmlGenericControl div = new HtmlGenericControl( "div" );
                div.Attributes.Add( "class", "randomArticle_container" );
                container.Controls.Add( div );

                HtmlGenericControl headline = new HtmlGenericControl( "h3" );
                headline.Attributes.Add( "class", "randomArticle_headline" );
                headline.InnerHtml = headlineParameter.Value;
                if ( !string.IsNullOrEmpty( headline.InnerHtml ) )
                {
                    div.Controls.Add( headline );
                }

                Random random = new Random();
                int index = random.Next( articles.Count );
                Article article = articles[index];

                HtmlGenericControl title = new HtmlGenericControl( "p" );
                title.Attributes.Add( "class", "randomArticle_title" );
                title.InnerHtml = article.Title;
                if ( !string.IsNullOrEmpty( title.InnerHtml ) )
                {
                    div.Controls.Add( title );
                }

                HtmlGenericControl body = new HtmlGenericControl( "p" );
                body.Attributes.Add( "class", "randomArticle_body" );
                body.InnerHtml = article.Body;
                div.Controls.Add( body );

                MembershipUser user = Membership.GetUser( new Guid( article.CreatedBy ) );
                Check.Require( user, string.Format( "Cannot find author of this article.  UserId= {0}", article.CreatedBy ) );
                HtmlGenericControl footer = new HtmlGenericControl( "p" );
                footer.Attributes.Add( "class", "randomArticle_footer" );
                footer.InnerHtml = string.Format( "Posted by {0} on {1}", user.UserName, article.CreatedOn );
                div.Controls.Add( footer );

            }

        }

        #endregion
    }
}
